package com.spzx.product.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌接口管理
 */
@Tag(name = "品牌接口管理")
@RestController
@RequestMapping(value = "/brand")
public class BrandController extends BaseController {
    @Autowired
    private IBrandService brandService;

    /**
     * 分页
     */
    @Operation(summary = "分页")
    @GetMapping("/list")
    public TableDataInfo list(Brand brand) {
        startPage(); //获取HttpServletRequest获取请求参数pageNum,pageSize,封装成Page对象,通过ThreadLocal绑定到线程上
        List<Brand> brandList = brandService.selectBrandList(brand);
        return getDataTable(brandList);
    }

    /**
     * 获取品牌详细信息
     */
    @Operation(summary = "根据id查询品牌对象")
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        Brand brand = brandService.selectById(id);
        return success(brand);
    }

    /**
     * 新增
     */
    @Operation(summary = "新增")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Validated Brand brand){
        brand.setCreateBy(SecurityUtils.getUsername());
        Integer result = brandService.add(brand);
        return toAjax(result);
    }

    /**
     * 修改品牌
     */
    @Operation(summary = "修改")
    @PutMapping
    public AjaxResult change(@RequestBody @Validated Brand brand){

        return toAjax(brandService.change(brand));
    }
    /**
     * 根据id集合删除品牌
     */
    @Operation(summary = "根据id集合删除品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(brandService.deleteById(ids));
    }

    @Operation(summary = "获取全部品牌")
    @GetMapping("/getBrandAll")
    public AjaxResult getAllBrand(){
        return success(brandService.selectBrandAll());
    }

}
